﻿using System;
using System.ComponentModel;
using System.Data;
using System.Windows.Forms;
using Telerik.WinControls.Data;
using Telerik.WinControls.UI;
using Zvezda.FSO2.DB;

namespace FSO
{
    public partial class UserControlSearchProductUnitForCostProtocol : UserControl, InterfaceSearchControl
    {
        private bool _openOnDblClick = false;
        private int _pid = -1;
        public UserControlSearchProductUnitForCostProtocol(int? pid)
        {
            InitializeComponent();
            if (pid.HasValue)
            {
                _pid = pid.Value;
            }
            radGridViewList.MultiSelect = true;
        }

        public UserControlSearchProductUnitForCostProtocol()
        :this(null)
        { 
        
        }

        public bool OpenOnDoubleClick
        {
            get
            {
                return _openOnDblClick;
            }
            set
            {
                _openOnDblClick = value;
            }
        }

        public bool EnableFiltering
        {
            get
            {
                return radGridViewList.EnableFiltering;
            }
            set
            {
                radGridViewList.EnableFiltering = value;
            }
        }

        public DataRow SelectedRow
        {
            get { throw new System.NotImplementedException(); }
        }

        public DataSet OwnerDataSet
        {
            get { return dataSetFSO1; }
            set { throw new System.NotImplementedException(); }
        }

        public GridViewSelectedRowsCollection SelectedRows
        {
            get { return radGridViewList.SelectedRows; }
        }

        public void RefreshList()
        {
            if (_pid == -1)
            {
                vieW_SEARCH_PU_FOR_COST_PROTOCOLTableAdapter1.Fill(dataSetFSO1.VIEW_SEARCH_PU_FOR_COST_PROTOCOL);
            }
            else
            {
                vieW_SEARCH_PU_FOR_COST_PROTOCOLTableAdapter1.FillByPid(dataSetFSO1.VIEW_SEARCH_PU_FOR_COST_PROTOCOL, _pid);
            }
        }

        private void InitializeGridView()
        {
            radGridViewList.ReadOnly = false;
            radGridViewList.EnableGrouping = false;
            radGridViewList.EnableFiltering = true;
            radGridViewList.MasterTemplate.EnableSorting = true;

            GridViewTextBoxColumn colProdName = new GridViewTextBoxColumn("PU_NAME", dataSetFSO1.VIEW_SEARCH_PU_FOR_COST_PROTOCOL.NAMEColumn.ColumnName);
            colProdName.HeaderText = "Наименование";
            colProdName.ReadOnly = true;

            GridViewDecimalColumn colCount = new GridViewDecimalColumn("PU_COUNT_TMP", dataSetFSO1.VIEW_SEARCH_PU_FOR_COST_PROTOCOL.COUNT_TMPColumn.ColumnName);
            colCount.HeaderText = "Кол.";
            colCount.DecimalPlaces = 0;
            colCount.Minimum = 1;
            colCount.ReadOnly = false;

            GridViewDecimalColumn colMaxCount = new GridViewDecimalColumn("PU_COUNT", dataSetFSO1.VIEW_SEARCH_PU_FOR_COST_PROTOCOL.COUNTColumn.ColumnName);
            colMaxCount.HeaderText = "Макс. кол.";
            colMaxCount.DecimalPlaces = 0;
            colMaxCount.ReadOnly = true;

            GridViewTextBoxColumn colMeter = new GridViewTextBoxColumn("PU_METER", dataSetFSO1.VIEW_SEARCH_PU_FOR_COST_PROTOCOL.METERColumn.ColumnName);
            colMeter.HeaderText = "Ед. Измерения";
            colMeter.ReadOnly = true;

            GridViewTextBoxColumn colCPStageNum = new GridViewTextBoxColumn("CP_NUM", dataSetFSO1.VIEW_SEARCH_PU_FOR_COST_PROTOCOL.CAL_P_NUMBERColumn.ColumnName);
            colCPStageNum.HeaderText = "Этапа";
            colCPStageNum.ReadOnly = true;

            GridViewTextBoxColumn colCPStageName = new GridViewTextBoxColumn("CP_NAME", dataSetFSO1.VIEW_SEARCH_PU_FOR_COST_PROTOCOL.CAL_P_NAMEColumn.ColumnName);
            colCPStageName.HeaderText = "Этап";
            colCPStageName.ReadOnly = true;

            GridViewTextBoxColumn colSDate = new GridViewTextBoxColumn("SDATE", dataSetFSO1.VIEW_SEARCH_PU_FOR_COST_PROTOCOL.CAL_P_START_DATEColumn.ColumnName);
            colSDate.HeaderText = "Начало этапа";
            colSDate.FormatString = "{0:d}";
            colSDate.ReadOnly = true;

            GridViewTextBoxColumn colEDate = new GridViewTextBoxColumn("EDATE", dataSetFSO1.VIEW_SEARCH_PU_FOR_COST_PROTOCOL.CAL_P_END_DATEColumn.ColumnName);
            colEDate.HeaderText = "Конец этапа";
            colEDate.FormatString = "{0:d}";
            colEDate.ReadOnly = true;

            //GridViewCheckBoxColumn colType = new GridViewCheckBoxColumn("PU_TYPE", dataSetFSO1.VIEW_SEARCH_PU_FOR_COST_PROTOCOL.TYPEColumn.ColumnName);
            //colType.HeaderText = "Товар";

            radGridViewList.MasterTemplate.Columns.Clear();
            radGridViewList.MasterTemplate.Columns.Add(colProdName);
            //radGridViewList.MasterTemplate.Columns.Add(colCount);
            radGridViewList.MasterTemplate.Columns.Add(colMaxCount);
            radGridViewList.MasterTemplate.Columns.Add(colMeter);
            radGridViewList.MasterTemplate.Columns.Add(colCPStageNum);
            //radGridViewList.MasterTemplate.Columns.Add(colCPStageName);
            radGridViewList.MasterTemplate.Columns.Add(colSDate);
            radGridViewList.MasterTemplate.Columns.Add(colEDate);

            radGridViewList.AllowAddNewRow = false;
            radGridViewList.AllowDeleteRow = false;

            SortDescriptor cpNum = new SortDescriptor(dataSetFSO1.VIEW_SEARCH_PU_FOR_COST_PROTOCOL.CAL_P_NUMBERColumn.ColumnName, ListSortDirection.Ascending);
            SortDescriptor name = new SortDescriptor(dataSetFSO1.VIEW_SEARCH_PU_FOR_COST_PROTOCOL.NAMEColumn.ColumnName, ListSortDirection.Ascending);
            radGridViewList.SortDescriptors.Add(name);
            radGridViewList.SortDescriptors.Add(cpNum);
            

            //Переключалка по запросам

            radGridViewList.MasterTemplate.DataSource = dataSetFSO1.VIEW_SEARCH_PU_FOR_COST_PROTOCOL;
        }

        private void UserControlSearchProductUnitForCostProtocol_Load(object sender, EventArgs e)
        {
            RefreshList();
            InitializeGridView();

            dataSetFSO1.VIEW_SEARCH_PU_FOR_COST_PROTOCOL.ColumnChanged += new DataColumnChangeEventHandler(VIEW_SEARCH_PU_FOR_COST_PROTOCOL_ColumnChanged);
        }

        void VIEW_SEARCH_PU_FOR_COST_PROTOCOL_ColumnChanged(object sender, DataColumnChangeEventArgs e)
        {

            if (e.Column.ColumnName == dataSetFSO1.VIEW_SEARCH_PU_FOR_COST_PROTOCOL.COUNT_TMPColumn.ColumnName)
            {
                int maxValue = ((DataSetFSO.VIEW_SEARCH_PU_FOR_COST_PROTOCOLRow) e.Row).COUNT;
                if ((System.Int32)e.ProposedValue > maxValue)
                {
                   ((DataSetFSO.VIEW_SEARCH_PU_FOR_COST_PROTOCOLRow)e.Row).COUNT_TMP = maxValue;
                }
            }
        }
    }
}
